package quick.portal.fragment.home;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;

import quick.common.AppContext;
import quick.common.AppManager;
import quick.common.UIHelper;
import quick.communication.AsyncHttpResponseHandler;
import quick.portal.R;
import quick.portal.activity.LoginActivity;
import quick.portal.data.APIResult;
import quick.portal.service.UserInfoService;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class HomeUserFragment extends Fragment implements OnClickListener {
	
	private EditText newPwd = null;
	private EditText validPwd = null;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.home_user_fragment, container,false);
		
		this.newPwd = (EditText)view.findViewById(R.id.newPassword);
		this.validPwd = (EditText)view.findViewById(R.id.validPassword);
		
		Button btnLogout = (Button)view.findViewById(R.id.btnLogout);
		btnLogout.setOnClickListener(this);
		
		Button btnModifyPwd = (Button)view.findViewById(R.id.btnModifyPwd);
		btnModifyPwd.setOnClickListener(this);

		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
    	this.clearInput();
	}
	

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btnLogout)
		{
			this.logout();
		}
		
		if(v.getId() == R.id.btnModifyPwd)
		{
			this.modifyPssword();
		}
	}
	
	private void clearInput()
	{
		this.newPwd.setText("");
    	this.validPwd.setText("");
    	
    	this.newPwd.requestFocus();
	}
	
	private void logout()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
		
		builder.setTitle(R.string.app_name);
		builder.setMessage("确定要退出本次登陆吗?");
		
		builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				AppManager.getAppManager().finishAllActivity();				
				AppContext.getInstance().clearUserInfo();	        	
	        	UIHelper.showActivity(getActivity(), LoginActivity.class);

			}
		});
		
		builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		
		builder.create().show();
	}
	
	private void modifyPssword()
	{
		final ProgressDialog pd = ProgressDialog.show(this.getActivity(), "", "正在修改密码...");
    	
    	if(TextUtils.isEmpty(newPwd.getText()))
		{
    		newPwd.setError("请填写新密码");
    		newPwd.requestFocus();
    		pd.dismiss();
    		return;
		}
    	
    	if(TextUtils.isEmpty(validPwd.getText()))
		{
    		validPwd.setError("请填写确认密码");
    		validPwd.requestFocus();
    		pd.dismiss();
    		return;
		}
    	
    	String newPassword = newPwd.getText().toString();
    	String newPassword2 = validPwd.getText().toString();
    	
    	if(!newPassword.equals(newPassword2))
		{
    		validPwd.setError("两次密码不一致");
    		validPwd.requestFocus();
    		pd.dismiss();
    		return;
		}
    	
    	new UserInfoService().modifyPassword(newPassword, new AsyncHttpResponseHandler()
        {
        	@Override
            public void onSuccess(String body) {

        		pd.dismiss();
        		        		
        		try
        		{
        			APIResult result = JSON.parseObject(body, APIResult.class);
        			
        			if(result.getReturnCode()==0)
    	        	{
        				String username = AppContext.getInstance().getUserName();

        				AppContext.getInstance().setUserInfo(username, result.getBody());
        	        	
        	        	clearInput();
    	        	}
    	        	
    	        	Toast.makeText(getActivity(),result.getMessage(), Toast.LENGTH_LONG).show();
        		}
	        	catch(Exception ex)
	        	{
	        		Toast.makeText(getActivity(),ex.getMessage(), Toast.LENGTH_LONG).show();
	        	}

        	}
        	
        	@Override
            public void onFailure(Request request, IOException ex) {
        		pd.dismiss();
        		Toast.makeText(getActivity(),ex.getMessage() , Toast.LENGTH_LONG).show();
            }
        });
	}
}
